package com.base.essearch.es.service.impl;

import com.base.essearch.es.bean.MovieEntity;
import com.base.essearch.es.bean.PageEntity;
import com.base.essearch.es.constant.MovieConstant;
import com.base.essearch.es.dao.IMovieDao;
import com.base.essearch.es.service.IMovieService;
import com.base.essearch.es.util.MovieDownloadUtil;
import com.base.essearch.page.PageDto;
import com.base.essearch.utils.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @date 2019-08-17
 */
@Service
public class MovieService extends BaseSearchServiceImpl<MovieEntity> implements IMovieService {

    @Autowired
    private IMovieDao movieDao;

    /**
     * 初始化执行的方法
     */
    @PostConstruct
    public void init() {
        //系统启动时,创建索引并设置mapping,如果未明显设置mapping,field注解配置不会生效
        movieDao.delIndex(MovieEntity.class);
        movieDao.createIndexAndMapping(MovieEntity.class);
        System.out.println("--------------初始化时创建索引" + MovieConstant.MOVIE_INDEX_NAME);
    }


    /**
     * 保存方法
     */
    @Override
    public void saveMovie() {
        MovieEntity entity = new MovieEntity();
        entity.setMovieId("27941");
        entity.setMovieName("沉默的证人");
        entity.setActors("张家辉  任贤齐  郭羡妮  郭晋安  吴卓羲  欧阳靖  杨紫  马书良  陈家乐  冯嘉怡  李成敏  明鹏");
        entity.setType("动作");
        entity.setArea("大陆");
        entity.setLanguage("国语");
        entity.setDirector("雷尼·哈林");
        entity.setReleaseDate("2019-08-17");
        entity.setScore(6.1);
        entity.setYear(2019);
        String documentId = movieDao.save(entity);
        System.out.println("----------保存成功------------------");
    }

    /**
     * 保存方法
     */
    @Override
    public void saveBatchMovie(List<?> list) {
        movieDao.saveBatch(list);
    }

    @Override
    public void startDownloadMovie() {
        System.out.println("--------------准备开始爬取--------------");
        List<MovieEntity> list = MovieDownloadUtil.startGetMovies();
        if (list.size() > 0) {
            for (MovieEntity entity : list) {
                entity.setCreateDate(new Date());
            }
        }
        System.out.println("--------------爬取完成--------------");
        movieDao.saveBatch(list);
        System.out.println("--------------保存完成--------------");
    }

    @Override
    public PageEntity getAllMovieForPage(String menu, String search, String selectType, int page, int pageSize, Boolean hl) {
        QueryBuilder booleanQueryBuilder = createBooleanQueryBuilder(menu, search, selectType);
        System.out.println(booleanQueryBuilder.toString());
        if (hl) {
            // 设置高亮,使用默认的highlighter高亮器
            HighlightBuilder highlightBuilder = createHighlightBuilder("movieName");
            PageDto<MovieEntity> movieEntityPage = pageQueryBuilder(booleanQueryBuilder, highlightBuilder, page, pageSize, MovieConstant.MOVIE_INDEX_NAME, MovieEntity.class);
            PageEntity entity = new PageEntity();
            entity.setDataList(movieEntityPage.getList());
            entity.setTotalCount(movieEntityPage.getTotal());
            entity.setTotalPage(movieEntityPage.getTotalPage());
            return entity;
        }


        Page pageInfo = movieDao.queryForPage(booleanQueryBuilder, page, pageSize, MovieEntity.class);
        PageEntity entity = new PageEntity();
        entity.setDataList(pageInfo.getContent());
        entity.setTotalCount(pageInfo.getTotalElements());
        entity.setTotalPage(pageInfo.getTotalPages());

        return entity;
    }

    private QueryBuilder createBooleanQueryBuilder(String menu, String search, String selectType) {
        QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolean has = false;
        if (StringUtils.isNotEmpty(menu) && !"*".equals(menu)) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("tag", menu).operator(Operator.AND));
            has = true;
        }
        if (StringUtils.isNotEmpty(search)) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("movieName", search).operator(Operator.AND));
            has = true;
        }
        if (has) {
            queryBuilder = boolQueryBuilder;
        }
        return queryBuilder;
    }


    @Override
    public List<MovieEntity> getAllMovie() {
        return (List<MovieEntity>) movieDao.queryForList(MovieEntity.class);
    }


    @Override
    public void deleteAllMovie() {
        movieDao.deleteAll(MovieEntity.class);
    }


    @Override
    public void deleteMovieById(String movieId) {
        movieDao.deleteById(MovieEntity.class, movieId);
    }


}
